package com.xiexin.web.controller;


import com.xiexin.common.core.domain.AjaxResult;
import com.xiexin.web.config.FastDFSClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


/**
 * 图片上传controller
 */
@PropertySource("classpath:picurl.properties")
@Controller
public class PictureController {
	
	@Value("${user.picurl}")
	private String IMAGE_SERVER_URL;

	@RequestMapping("/pic/upload")
	@ResponseBody
	public AjaxResult picUpload(@RequestParam("file") MultipartFile imgFile ) {
		System.out.println("uploadFile = " + imgFile);
		System.out.println("访问到了");
		try {
			//接收上传的文件
			//取扩展名
			String originalFilename = imgFile.getOriginalFilename();
			String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
			//上传到图片服务器
			FastDFSClient fastDFSClient = new FastDFSClient("classpath:client.conf");
			String url = fastDFSClient.uploadFile(imgFile.getBytes(), extName);
			url = IMAGE_SERVER_URL+"/" + url;
			//响应上传图片的url
			System.out.println("url = " + url);
			return AjaxResult.success(url);
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxResult.error("400","服务器异常");
		}
	}
}
